<?php namespace Member\Controller; 

use Hdphp\Controller\Controller;
use Member\Model\Safe;

class SafeController extends Controller
{
	protected $db;

	//构造函数
	public function __init()
	{
		$this->db = new Safe;
	}
	
    //动作
    public function safe(){
    	if(IS_POST){
    		$id = Q('post.id');
    		$user = $this->db->only($id);
    		//判断旧密码
    		if(md5($_POST['password_old']) != $user['password']){
    			View::error('旧密码不正确');
    			return false;
    		}
    		//判断新密码两次是否正确
    		if($_POST['password'] != $_POST['password_c']){
    			View::error('两次密码输入不正确');
    			return false;
    		}
    		//判断验证码是否正确
    		if(strtoupper($_POST['code']) != $_SESSION['code']){
    			View::error('验证码不正确');
    			return false;
    		}
    		$data['id'] = $id;
    		$data['password'] = md5($_POST['password']);
    		if($this->db->edit($data))
    		{
    			View::success('修改密码成功');
    		}

    	}else{
    		View::make();
    	}
        
    }

    public function code()
    {
    	Code::make();
    }
}
